package com.sky.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealList = setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<Setmeal> p = (Page) setmealList;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> Dishes = setmealDTO.getSetmealDishes();
        if (Dishes != null && Dishes.size() > 0){
            for (SetmealDish dish : Dishes) {
                dish.setSetmealId(setmealId);
            }
            setmealDishMapper.insertDishes(Dishes);
        }
    }

    @Override
    @Transactional
    public void delete(Long[] ids) {
        for (Long id : ids){
            Setmeal setmeal = setmealMapper.getbyId(id);
            if (setmeal.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids){
            setmealMapper.deletebyId(id);
            setmealDishMapper.delete(id);
        }
    }

    @Override
    public SetmealVO getbyId(Long id) {
        Setmeal setmeal= setmealMapper.getbyId(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.list(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void updateById(SetmealDTO setmealDTO) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateById(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> Dishes = setmealDTO.getSetmealDishes();
        if (Dishes != null && Dishes.size() > 0){
            for (SetmealDish dish : Dishes) {
                dish.setSetmealId(setmealId);
            }
            setmealDishMapper.insertDishes(Dishes);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //判断当前状态
        List<Long> dishIds = setmealDishMapper.getByDishId(id);
        for (Long dishId : dishIds){
            Long dishstatus=dishMapper.getBystatus(dishId);
            if (dishstatus==0){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.startOrStop(status,id);
    }

    @Override
    public List<Setmeal> list(Long categoryId) {
        List<Setmeal> list = setmealMapper.list(categoryId);
        return list;
    }

    @Override
    public List<DishItemVO> DishgetbyId(Long id) {
        List<DishItemVO> dishItemVO = setmealMapper.getdishItemVO(id);
        return dishItemVO;
    }
}
